package com.sgb.knowledgeshare.view.adapter;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;

/**
 * 这个是使用了预加载的fragmentPageAdapter
 */
public class ShareCommPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<? extends Fragment> items;
    private String[] mTitles;
    private FragmentManager fm;

    public ShareCommPagerAdapter(FragmentManager fm, ArrayList<? extends Fragment> items, String[] mTitles) {
        super(fm);
        this.fm = fm;
        this.items = items;
        this.mTitles = mTitles;
    }

    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public Fragment getItem(int position) {
        return items.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }

    @Override
    public Fragment instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container,
                position);
        fm.beginTransaction().show(fragment).commitAllowingStateLoss();
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container, position, object);
        Fragment fragment = (Fragment) object;
        fm.beginTransaction().hide(fragment).commit();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;

    }
}
